import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { usePermissStore } from '../store/permiss';
import Home from '../views/home.vue';

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/charts',
    },
    {
        path: '/',
        name: 'Home',
        component: Home,
        children: [
            {
                path: '/dashboard',
                name: 'dashboard',
                meta: {
                    title: '数据大屏',
                    permiss: '1',
                },
                component: () => import(/* webpackChunkName: "dashboard" */ '../views/charts.vue'),
            },
            {
                path: '/user',
                name: 'user',
                meta: {
                    title: '用户管理',
                    permiss: '2',
                },
                component: () => import('../pages/user.vue'),
            },
            {
                path: '/company',
                name: 'company',
                meta: {
                    title: '公司管理',
                    permiss: '3',
                },
                component: () => import('../pages/company.vue'),
            },
            {
                path: '/orderForm',
                name: 'orderForm',
                meta: {
                    title: '订单管理',
                    permiss: '4',
                },
                component: () => import('../pages/orderForm.vue'),
            },
            {
                path: '/demand',
                name: 'demand',
                meta: {
                    title: '需求信息',
                    permiss: '5',
                },
                component: () => import('../pages/demand.vue'),
            },
            {
                path: '/housekeeper',
                name: 'housekeeper',
                meta: {
                    title: '家政员信息',
                    permiss: '6',
                },
                component: () => import('../pages/housekeeper.vue'),
            },
            {
                path: '/serviceType',
                name: 'serviceType',
                meta: {
                    title: '服务类型管理',
                    permiss: '7',
                },
                component: () => import('../pages/serviceType.vue'),
            },
            {
                path: '/system',
                name: 'system',
                meta: {
                    title: '系统管理',
                    permiss: '8',
                },
                children: [
                    {
                        path: '/resource',
                        name: 'resource',
                        meta: {
                            title: '资源管理',
                            permiss: '27',
                        },
                        component: () => import('../pages/resource.vue'),
                    },
                    {
                        path: '/permission',
                        name: 'permission',
                        meta: {
                            title: '权限管理',
                            permiss: '28',
                        },
                        component: () => import('../pages/permission.vue'),
                    }
                ]
            },
            {
                path: '/housekeeperForm',
                name: 'housekeeperForm',
                meta: {
                    title: '新增家政员',
                    permiss: '9',
                },
                component: () => import('../pages/housekeeperForm.vue'),
            },
            {
                path: '/addressBook',
                name: 'addressBook',
                meta: {
                    title: '地址簿',
                    permiss: '10',
                },
                component: () => import('../pages/addressBook.vue'),
            },
            {
                path: '/demandForm',
                name: 'demandForm',
                meta: {
                    title: '发布需求',
                    permiss: '11',
                },
                component: () => import('../pages/demandForm.vue'),
            },
            {
                path: '/createOrderForm',
                name: 'createOrderForm',
                meta: {
                    title: '签约',
                    permiss: '11',
                },
                component: () => import('../pages/createOrderForm.vue'),
            },
            {
                path: '/charts',
                name: 'basecharts',
                meta: {
                    title: '数据大屏',
                },
                component: () => import(/* webpackChunkName: "charts" */ '../views/charts.vue'),
            },
            {
                path: '/tabs',
                name: 'tabs',
                meta: {
                    title: 'tab标签',
                },
                component: () => import(/* webpackChunkName: "tabs" */ '../views/tabs.vue'),
            },
            // {
            //     path: '/userCenter',
            //     name: 'user',
            //     meta: {
            //         title: '个人中心',
            //     },
            //     component: () => import(/* webpackChunkName: "user" */ '../views/user.vue'),
            // },
            {
                path: '/import',
                name: 'import',
                meta: {
                    title: '导入Excel',
                },
                component: () => import(/* webpackChunkName: "import" */ '../views/import.vue'),
            },
        ],
    },
    {
        path: '/login',
        name: 'Login',
        meta: {
            title: '登录',
        },
        component: () => import(/* webpackChunkName: "login" */ '../views/login.vue'),
    },
    {
        path: '/403',
        name: '403',
        meta: {
            title: '没有权限',
        },
        component: () => import(/* webpackChunkName: "403" */ '../views/403.vue'),
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | 智慧家政`;
    next();
    // const role = localStorage.getItem('account');
    const permiss = usePermissStore();
    // if (!role && to.path !== '/login') {
    //     next('/login');
    // } else if (to.meta.permiss && !permiss.key.includes(to.meta.permiss)) {
    //     // 如果没有权限，则进入403
    //     next('/403');
    // } else {
    //     next();
    // }
});

export default router;
